import os
import re
import sys
import time
import jieba
import random
import jieba.posseg as pseg

from pymongo import MongoClient
from selenium import webdriver
from bs4 import BeautifulSoup


def new_driver(headless = False):
    options = webdriver.ChromeOptions()

    if headless:
        options.add_argument('--headless')
        options.add_argument('--disable-gpu')
        options.add_argument('--disable-images')

    desired_capabilities = options.to_capabilities()
    desired_capabilities['loggingPrefs'] = {'performance': 'ALL'}

    chrome_driver_path = os.getcwd()[:os.getcwd().rfind('/') + 1] + 'chromedriver'
    driver = webdriver.Chrome(chrome_driver_path, desired_capabilities = desired_capabilities)

    return driver


def crawl_menus():
    driver = new_driver(headless = True)
    driver.get('https://www.lagou.com/')

    time.sleep(3)

    soup = BeautifulSoup(driver.page_source, 'lxml')
    menus = []

    for a in soup.find(class_ = 'menu_box').select('a'):
        if not a.get('href'):
            continue

        if re.findall('https://www.lagou.com/zhaopin/.+/', a.get('href')):
            menus.append(a.get_text())

    random.shuffle(menus)

    return menus


def identifier(item):
    name = item['personal_info']['name']
    sex = item['personal_info']['sex']
    age = item['personal_info']['age']
    edu = item['personal_info']['edu']
    address = item['personal_info']['address']

    identifier = name + '|' + sex + '|' + age + '|' + edu + '|' + address + '|'

    for i in item['work_experience']:
        if 'company' in i:
            identifier += i['company'] + '|'

        if 'title' in i:
            identifier += i['title'] + '|'

    return identifier


with open('/Users/xuchaosheng/Workspace/cv_match/core.txt', 'r') as file:
    techwords = [w.strip().lower() for w in file.readlines()]

    for w in techwords:
        jieba.add_word(w, 10000, 'n')


def jd_token(item):
    text = ''

    text += item['self_introduce']

    for e in item['work_experience']:
        text += '|' + e['describe']

    for e in item['proj_experience']:
        text += '|' + e['describe']
        text += '|' + e['responsibility']

    return list(set([word for word, flag in pseg.cut(text.lower()) if len(word) > 1]))
